const Compiler = require('./Compiler.js');
const NodeEnvironmentPlugin = require('./node/NodeEnvironmentPlugin.js')
const WebpackOptionsApply = require('./WebpackOptionsApply.js');

const webpack = function (options) {
  // 实例化compiler对象
  let compiler = new Compiler(options.context)
  compiler.options = options

  // 让compiler具备文件读写功能
  new NodeEnvironmentPlugin().apply(compiler)

  // 挂载所有plugin到compiler
  if (options.plugins && Array.isArray(options.plugins)) {
    for (const plugin of options.plugins) {
      plugin.apply(compiler)
    }
  }

  new WebpackOptionsApply().process(options, compiler)

  return compiler;
}

module.exports = webpack